<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="父级目录" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
              <a-tree-select 
                  v-model="model.parentId"
                  :tree-data="treeData"
                  placeholder="请选择父级目录"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  allow-clear
                  show-search
                   tree-node-filter-prop="title"
                  :replaceFields="{children:'children', title:'name', key:'id', value: 'id' }"
                >
              </a-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入分类名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入编码"  ></a-input>
            </a-form-model-item>
          </a-col>  
          <a-col :span="24">
            <a-form-model-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
                <a-radio-group name="radioGroup"  v-model="model.status">
                  <a-radio :value="0">
                    启用
                  </a-radio>
                  <a-radio :value="1">
                    禁用
                  </a-radio>
               </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input-number v-model="model.orderNo" :defaultValue="1" :precision="0" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" :max-length="125" ></a-input>
            </a-form-model-item>
          </a-col>
      
       
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'StdSampleTypeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           
           name: [
              { required: true, message: '请输入分类名称!'},
           ],
           code: [
              { required: true, message: '请输入分类编码!'},
           ],
          
           status: [
              { required: true, message: '请输入状态0启动1禁用!'},
           ],
           orderNo: [
              { required: true, message: '请输入排序!'},
           ],
           
        },
         treeData:[],

        url: {
          add: "/stdSampleType/add",
          edit: "/stdSampleType/edit",
          queryById: "/stdSampleType/queryById",
          getTree:"/stdSampleType/getTree",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted(){
      this.getTree()
    },
    methods: {
      getTree(){
        getAction(this.url.getTree,{}).then(res=>{
          if(res.code== 200){
             this.treeData =  res.result
          }else{
            this.$message.warning(res.message)
          }
        })

      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        record.parentId = !record.parentId ? null : record.parentId
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>